const ajax = require('../../../utils/ajax/ajax.js')
Page({
  data: {
    bannerList: [],
    projectList: [],
    articleList: [],
    tabToggle: true,
    pages: 1,
    projectLoading: true,
    articleLoading: true
  },
  onLoad() {
    this.init() 
  },

  init() {
    // const token = wx.getStorageSync('token')
    // if (!token) {
    //   wx.navigateTo({
    //     url: '/pages/login/login',
    //   })
    //   return
    // }
    this.getNewsList()
    this.getBannerList()
    this.getProjectList()
  },

  onUserLogin() {
    this.getBannerList()
    this.getNewsList()
    this.getProjectList()
  },

  getBannerList(callback) {
    const that = this
    ajax.post({
      url: '/wx/getBanner',
      success(res) {
        that.setData({
          bannerList: res.data
        })
      },
      complete() {
        typeof callback === 'function' && callback()
      }
    })
  },
  getNewsList(callback) {
    const that = this
    ajax.post({
      url: '/wx/getNewInformation',
      data: {
        page: 1,
        limit: 10
      },
      success(res) {
        that.setData({
          articleList: res.data.list
        })
      },
      complete() {
        typeof callback === 'function' && callback()
      }
    })
  },
  getProjectList(callback) {
    const that = this
    ajax.post({
      url: '/wx/getProjectList',
      data: {
        page: 1,
        limit: 10
      },
      success(res) {
        that.setData({
          projectList: res.data.list
        })
      },
      complete() {
        typeof callback === 'function' && callback()
      }
    })
  },

  // 切换 tab
  tabSwitch(e) {
    const tabToggle = e.currentTarget.dataset.toggle
    this.setData({
      tabToggle
    })
  },
  toProjectDetails(e) {
    const proId = e.currentTarget.dataset.proid
    wx.navigateTo({
      url: '/pages/Index/projectDetails/projectDetails?proId=' + proId,
    })
  },
  // 跳转 文章详情
  toArticleDetails(e) {
    const newsId = e.currentTarget.dataset.newsid
    wx.navigateTo({
      url: '/pages/Index/articleDetails/articleDetails?newsId=' + newsId,
    })
  },
  // 跳转 申请会员
  toApplyVip() {
    ajax.post({
      url: '/wx/getMyInfo',
      success(res) {
        const memberVerify = res.data.memberVerify
        // 去申请会员
        if (!memberVerify) {
          wx.navigateTo({
            url: '/pages/Index/applyVip/applyVip',
          })
          return
        }

        const { verify_url, status } = memberVerify

        // 补填会员凭证
        if (verify_url === '' && status === 0) {
          wx.navigateTo({
            url: '/pages/Index/attestationNoInfo/attestationNoInfo',
          })
          return
        }

        // 提示身份认证中
        if (verify_url && status === 0) {
          wx.navigateTo({
            url: '/pages/Index/attestationInfo/attestationInfo',
          })
          return
        }

        wx.navigateTo({
          url: '/pages/Index/applyVip/applyVip',
        })

      }
    })
  },

  toPrijectList() {
    wx.navigateTo({
      url: '/pages/Index/projectList/projectList',
    })
  },

  toVenue() {
    wx.navigateTo({
      url: '/pages/Index/venue/venue',
    })
  },

  onReachBottom () {
    const tabToggle = this.data.tabToggle
    if (tabToggle) {
      (this.data.pages) ++;
      this.reachBottomProject()
      return
    }

    if(!tabToggle) {
      (this.data.pages) ++;
      this.reachBottomArticle()
      return
    }
  },

  reachBottomProject() {
    const that = this
    ajax.post({
      url: '/wx/getProjectList',
      data: {
        page: that.data.pages,
        limit: 10
      },
      success(res) {
        let projectList = res.data.list
        if (projectList.length === 0) {
          that.setData({
            projectLoading: true
          })
          return
        }
        
        if (that.data.pages === 1) {
          that.setData({
            projectList: projectList
          })
          return
        }

        if (that.data.pages !== 1) {
          let newList = that.data.projectList.concat(projectList)
          that.setData({
            projectList: newList
          })
          return
        }

      }
    })
  },

  reachBottomArticle() {
    const that = this
    ajax.post({
      url: '/wx/getNewInformation',
      data: {
        page: that.data.pages,
        limit: 10
      },
      success(res) {
        let articleList = res.data.list
        if (articleList.length === 0) {
          that.setData({
            articleLoading: true
          })
          return
        }
        
        if (that.data.pages === 1) {
          that.setData({
            articleList: articleList
          })
          return
        }

        if (that.data.pages !== 1) {
          let newList = that.data.articleList.concat(articleList)
          that.setData({
            articleList: newList
          })
          return
        }

      }
    })
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.getProjectList()
    this.getNewsList()
    this.getBannerList()
    wx.stopPullDownRefresh()
  }
})